<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$building_id = mysql_real_escape_string(strip_tags($_GET['id']));

$get_building_data = mysql_query("SELECT * FROM buildings WHERE id = $building_id");
$building_name = mysql_result($get_building_data,0,'building_name');
$abbreviation = mysql_result($get_building_data,0,'abbreviation');

?>
<p>
	<a href="index.php">&lt;&lt; Back to Timeslots Portal</a>
</p>
<?php
echo "<h1>$building_name Timeslots Portal</h1>";
echo '<p>';
//creates link/button for add timeslot
echo "<a href='add.php?bid=$building_id'>Add timeslot</a>";
echo '</p>';
//counting variable
$i = 0;

//select statement
$query="SELECT * FROM time_slots WHERE building_id = $building_id ORDER BY hours_per_week DESC, Monday DESC, Tuesday DESC, Wednesday DESC, Thursday DESC, Friday DESC, Saturday DESC, Sunday DESC, start_time ASC, end_time ASC";
$result=mysql_query($query);

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

if ($num_rows > 0)
{
	echo '<p>';

	//start table
	echo "<table class=\"postSchedule\">";
	
		//create table row with column ID's
		echo "<tr>";
			echo "<td style='text-align: center'><b>Hours/Week</b></td>";
			echo "<td style='text-align: center'><b>Start Time</b></td>";
			echo "<td style='text-align: center'><b>End Time</b></td>";
			echo "<td style='text-align: center'>Monday</td>";
			echo "<td style='text-align: center'>Tuesday</td>";
			echo "<td style='text-align: center'>Wednesday</td>";
			echo "<td style='text-align: center'>Thursday</td>";
			echo "<td style='text-align: center'>Friday</td>";
			echo "<td style='text-align: center'>Saturday</td>";
			echo "<td style='text-align: center'>Sunday</td>";
			echo "<td style='text-align: center'></td>";
			echo "<td style='text-align: center'></td>";
		echo "</tr>";

	//loop to walk through and print to screen all entries in timeslots table
	$row_count = 0;
	while ($i < $num_rows) 
	{
		$row_count++;
		//store each timeslotname and abbreviation in a scalar
		$timeslot_id=mysql_result($result,$i,"id");
		$starttime=mysql_result($result,$i,"start_time");
		$endtime=mysql_result($result,$i,"end_time");
		$hours_per_week=mysql_result($result,$i,"hours_per_week");
		$mon=mysql_result($result,$i,"Monday");
		$tues=mysql_result($result,$i,"Tuesday");
		$weds=mysql_result($result,$i,"Wednesday");
		$thurs=mysql_result($result,$i,"Thursday");
		$fri=mysql_result($result,$i,"Friday");
		$sat=mysql_result($result,$i,"Saturday");
		$sun=mysql_result($result,$i,"Sunday");
	
		//generate table row for each timeslot on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td style='text-align: center'>$hours_per_week</td>";	
			echo "<td style='text-align: center'>$starttime</td>";		
			echo "<td style='text-align: center'>$endtime</td>";
			
			echo "<td style='text-align: center'>";
			if ($mon == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($tues == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($weds == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($thurs == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($fri == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($sat == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			echo "<td style='text-align: center'>";
			if ($sun == 1)
			{
				echo "<b>X</b>";
			}
			else
			{
				echo "-";
			}
			echo "</td>";
			
			
			echo "<td>";		//cell containing form for edit timeslot link/button
				echo "<a href='edit.php?id=$timeslot_id'>Edit</a>";
			echo "</td>";
		
			echo "<td>";		//cell containing form for delete timeslot link/button
				echo "<a href='delete.php?id=$timeslot_id'>Delete</a>";
			echo "</td>";
	
		echo "</tr>";
		
		//increment counting variable
		$i++;
	}

	//end table tag
	echo "</table>";

	echo '</p>';

}
else
{
	echo "<p><b>NO TIMESLOTS CURRENTLY ASSIGNED TO BUILDING</b></p>";
}



?>


<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>